package com.jeesite.modules.digitalize.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bimface.api.bean.response.FileIntegrateBean;
import com.jeesite.modules.digitalize.config.Constants;
import com.jeesite.modules.digitalize.dao.DigitalizeComponentIntegrationDao;
import com.jeesite.modules.digitalize.entity.DigitalizeComponentIntegration;
import com.jeesite.modules.digitalize.entity.DigitalizeComponentNote;
import com.jeesite.modules.digitalize.entity.dto.DigitalizeComponentIntegrationDTO;
import com.jeesite.modules.digitalize.entity.dto.DigitalizeComponentIntegrationFileDTO;
import com.jeesite.modules.digitalize.util.bimface.BimFaceUtils;
import com.jeesite.modules.digitalize.util.response.ResultVo;
import com.jeesite.modules.sys.utils.UserUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (DigitalizeComponentIntegration)表服务实现类
 *
 * @author xk
 * @since 2024-01-03 14:29:24
 */
@Service("digitalizeComponentIntegrationService")
public class DigitalizeComponentIntegrationService extends ServiceImpl<DigitalizeComponentIntegrationDao, DigitalizeComponentIntegration> {

    @Resource
    private BimFaceUtils bimFaceUtils;

    @Resource
    @Lazy
    private DigitalizeComponentNoteService digitalizeComponentNoteService;

    public ResultVo add(DigitalizeComponentIntegrationDTO dto) {
        //bimface 集成
        FileIntegrateBean integration = bimFaceUtils.integration(dto);
        if(integration == null){
            return ResultVo.failure("集成失败!");
        }
        System.out.println(integration.getIntegrateId());
        DigitalizeComponentIntegration integrationEntity = new DigitalizeComponentIntegration();
        integrationEntity.setIntegrateld(integration.getIntegrateId().toString());
        integrationEntity.setIntegrateName(dto.getName());
        integrationEntity.setProjectCode(dto.getProjectCode());
        integrationEntity.setIntegrateldStatus(Constants.INTEGRATION_STATUS_0);
        StringBuilder sb = new StringBuilder();
        boolean flag = true;
        for (DigitalizeComponentIntegrationFileDTO fileDTO : dto.getFileList()) {
            if (flag) {
                sb.append(fileDTO.getFileId());
                flag = false;
            } else {
                sb.append(",").append(fileDTO.getFileId());
            }
        }
        integrationEntity.setComponentIdList(sb.toString());
        integrationEntity.setDelStatus(Constants.DEL_STATUA_0);
        integrationEntity.setCreateBy(UserUtils.getUser().getId());
        //新增数据
        this.save(integrationEntity);
        //复制所有当前 标签信息
        LambdaQueryWrapper<DigitalizeComponentNote> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(DigitalizeComponentNote::getComponentId, dto.getFileList().stream().map(item -> item.getFileId()).collect(Collectors.toList()));
        List<DigitalizeComponentNote> notes = digitalizeComponentNoteService.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(notes)) {
            List<DigitalizeComponentNote> noteList = notes.stream().map(item -> {
                item.setId(null);
                item.setComponentId(integrationEntity.getIntegrateld());
                item.setFileType(1);
                return item;
            }).collect(Collectors.toList());
            digitalizeComponentNoteService.saveBatch(noteList);
        }
        return ResultVo.success();
    }

    public ResultVo removeRecycleByIds(List<Long> idList) {
        //删除进入回收站
        LambdaUpdateWrapper<DigitalizeComponentIntegration> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(DigitalizeComponentIntegration::getId, idList);
        updateWrapper.set(DigitalizeComponentIntegration::getDelStatus, Constants.DEL_STATUA_2);
        this.update(updateWrapper);
        return ResultVo.success();
    }

    public void updateByIdAndDelStatus(DigitalizeComponentIntegration integration) {
        this.baseMapper.updateByIdAndDelStatus(integration);
    }

}

